package com.zzl.car.controller;

import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.Notice;
import com.zzl.car.service.ZzwNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@CrossOrigin
@RestController
public class ZzwNoticeController {

    @Autowired
    private ZzwNoticeService noticeService;

    // 获取公告信息
    @GetMapping("/getNotice")
    public PageInfo<Notice> getNotice(@RequestParam(value = "title",required = false)String title,
                                      @RequestParam(value = "state",required = false)Integer state,
                                      @RequestParam(value = "currentPage",defaultValue = "1")int currentPage){
        System.out.println(title);
        System.out.println(state);
        return noticeService.getNotice(title,state,currentPage);
    }

    // 删除公告
    @GetMapping("/deleteNotice")
    public String deleteNotice(@RequestParam("id") Integer id){
        return noticeService.deleteNotice(id);
    }

    // 添加公告
    @PostMapping("addNotice")
    public String addNotice(@RequestBody Map map){
        Notice notice = new Notice();
        notice.setTitle(map.get("title").toString());
        notice.setContent(map.get("content").toString());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        long tt = timestamp.getTime() + 8*60*60*1000;
        timestamp.setTime(tt);
        System.out.println(timestamp);
        notice.setTime(timestamp);

        return noticeService.addNotice(notice);
    }
    // 修改公告
    @PostMapping("modifyNotice")
    public String modifyNotice(@RequestBody Notice notice){

        return noticeService.modifyNotice(notice);
    }
}
